package com.behelpful.demo.quartz.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.behelpful.common.core.result.R;
import com.behelpful.demo.quartz.entity.SystemJob;
import com.behelpful.demo.quartz.enums.JobStatusEnum;
import com.behelpful.demo.quartz.service.SystemJobService;
import com.behelpful.demo.quartz.utils.IQuartzUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 定时任务管理
 *
 * @author Alay
 * @date 2021-10-31 18:30
 * @see {cron表达式 https://www.cnblogs.com/Alay/p/15488175.html}
 * @see {参考https://gitee.com/52itstyle/spring-boot-quartz}
 * @since
 */
@Api(value = "task", tags = "定时任务管理")
@RestController
@RequestMapping(value = "/task")
public class TaskController {


    @Autowired
    private Scheduler scheduler;
    @Autowired
    private SystemJobService systemJobService;


    @GetMapping(value = "/page")
    @ApiOperation(value = "分页查询定时任务", notes = "分页查询定时任务")
    public R page(Page page, SystemJob systemJob) {
        page = systemJobService.page(page, Wrappers.lambdaQuery(systemJob));
        return R.ok(page);
    }


    @PostMapping(value = "/addJob")
    @ApiOperation(value = "添加定时任务", notes = "添加定时任务")
    public R addJob(@RequestBody SystemJob systemJob) {
        // 这里示例代码没有对前端的参数进行校验,实际开发中需要对参数进行校验
        boolean isOk = systemJobService.save(systemJob);
        // 新增的定时任务为直接运行状态
        if (isOk && JobStatusEnum.RUNNING == systemJob.getJobStatus()) {
            IQuartzUtil.addNewJob(systemJob, scheduler);
        }
        return R.ok(isOk);
    }


    @PutMapping(value = "/updateJob")
    @ApiOperation(value = "修改定时任务", notes = "修改定时任务")
    public R updateJob(@RequestBody SystemJob systemJob) {
        SystemJob jobInDb = this.systemJobService.getById(systemJob.getJobId());
        // 新增的定时任务为暂停状态
        boolean isOk;
        if (JobStatusEnum.RUNNING == systemJob.getJobStatus()) {
            // 移除定时任务
            IQuartzUtil.removeJob(systemJob, scheduler);

            isOk = systemJobService.updateById(systemJob);
            if (isOk) {
                // 添加新的定时任务
                IQuartzUtil.addNewJob(systemJob, scheduler);
            }
        } else {
            isOk = systemJobService.updateById(systemJob);
        }
        return R.ok(isOk);
    }


    @PutMapping(value = "/pauseJob/{jobId}")
    @ApiOperation(value = "暂停定时任务", notes = "暂停定时任务")
    public R pauseJob(@PathVariable(value = "jobId") Integer jobId) {
        SystemJob systemJob = systemJobService.getById(jobId);
        // 暂停任务
        IQuartzUtil.pauseJob(systemJob, scheduler);

        // 修改数据库
        systemJob.setJobStatus(JobStatusEnum.PAUSE);
        boolean isOk = systemJobService.updateById(systemJob);

        return R.ok(isOk);
    }


    @PostMapping("/runJob/{id}")
    @ApiOperation(value = "立刻执行定时任务")
    public R runJob(@PathVariable("id") Integer jobId) {
        SystemJob systemJob = this.systemJobService.getById(jobId);
        boolean isOk = IQuartzUtil.runJob(systemJob, scheduler);
        if (!isOk) {
            return R.failed();
        }
        return R.ok(true);
    }


    @DeleteMapping(value = "/removeJob/{jobId}")
    @ApiOperation(value = "移除定时任务", notes = "移除定时任务")
    public R removeJob(@PathVariable(value = "jobId") Integer jobId) {
        SystemJob systemJob = systemJobService.getById(jobId);

        if (JobStatusEnum.RUNNING == systemJob.getJobStatus()) {
            // 运行中的任务需要先移除
            IQuartzUtil.removeJob(systemJob, scheduler);
        }
        systemJob.setJobStatus(JobStatusEnum.REMOVED);
        boolean isOk = systemJobService.updateById(systemJob);
        return R.ok(isOk);
    }


}
